#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <QtMath>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    if (ui) {
        delete ui;
        ui = nullptr;
    }
}

void Widget::on_pushButtonCalc_clicked()
{
    int num = ui->lineEditNum->text().toInt(); // 数量：字符串转换为整数
    float price = ui->lineEditPrice->text().toFloat(); // 单价：字符串转换为浮点数

    float total = num * price;
    QString str;
    str = str.setNum(total, 'f', 2); //浮点数2位小数
    ui->lineEditTotal->setText(str);
}

void Widget::on_pushButtonDebug_clicked()
{
    //======字符串转换为整数
    QString str1;
    bool ok1 = false;
    int num1;
    //1. 默认十进制转换
    str1 = "153";
    num1 = str1.toInt(&ok1);
    qDebug("str = %s", str1.toLocal8Bit().data());
    qDebug("ok = %d, N = %d", ok1, num1);

    //2. 使用十六进制转换
    QString str2 = "FF";
    bool ok2 = false;
    int num2 = str2.toInt(&ok2, 16);    //按16进制转换
    qDebug("str = %s", str2.toLocal8Bit().data());
    qDebug("ok = %d, N = %d", ok2, num2);

    //3. 使用二进制转换
    QString str3 = "10110111";
    bool ok3 = false;
    int num3 = str3.toInt(&ok3, 2);    //按2进制转换
    qDebug("str = %s", str3.toLocal8Bit().data());
    qDebug("ok = %d, N = %d", ok3, num3);

    //4. 按C语言规则
    QString str4 ="0x5F";
    bool ok4 = false;
    int num4 = str4.toInt(&ok4,0);    //按C语言规则
    qDebug("str = %s",str4.toLocal8Bit().data());
    qDebug("ok = %d, N = %d", ok4, num4);


    //=====setNum()函数,整数
    QString str5;
    int num5 = 243;
    str5.setNum(num5);
    qDebug("N = %d", num5);
    qDebug("str = %s", str5.toLocal8Bit().data());

    str5.setNum(num5, 16);
    qDebug("str= %s", str5.toLocal8Bit().data());

    str5.setNum(num5, 2);
    qDebug("str= %s", str5.toLocal8Bit().data());


    //=====setNum()函数,浮点数
    QString str6;
    double num6 = 1245.2783;
    qDebug("num = %f", num6);

    str6.setNum(num6, 'f', 5);    //小数点后5位, str= "1245.27830"
    qDebug("str = %s", str6.toLocal8Bit().data());

    str6.setNum(num6, 'E', 5);    //小数点后是5位, str= "1.24528E+03"
    qDebug("str = %s", str6.toLocal8Bit().data());

    str6.setNum(num6,'g', 5);    //整数和小数位总共5位, str= "1245.3"
    qDebug("str = %s", str6.toLocal8Bit().data());

    str6.setNum(num6,'g', 3);    //整数和小数位总共3位, str= "1.25e+03"
    qDebug("str = %s", str6.toLocal8Bit().data());


    //=====静态函数number()函数
    int num7 =245;
    QString str7 =QString::number(203);   //转换为10进制字符串，str="245";
    qDebug("N = %d", num7);
    qDebug("str = %s", str7.toLocal8Bit().data());

    str7 = QString::number(num7, 16);         //转换为16进制字符串，str="f5";
    qDebug("str = %s", str7.toLocal8Bit().data());

    double  num8 = 365.263;
    qDebug("num = %f", num8);

    str7 = QString::number(num8, 'f', 4);  //小数点后4位, str= "365.2630"
    qDebug("str = %s", str7.toLocal8Bit().data());

    str7 = QString::number(num8, 'E', 4);  //小数点后是4位, str= "3.6526E+02"
    qDebug("str = %s", str7.toLocal8Bit().data());

    //====测试静态函数 asprintf()
    QString str8 = QString::asprintf("Year=%d,\tMonth=%2d,\tDay=%2d", 2021, 6, 12);
    qDebug(str8.toLocal8Bit().data());

    //QString UPC = "UPC";
    QString UPC = "石油大学";     //不支持变量中有汉字
    str8 = QString::asprintf("Hello, 欢迎来到 %s", UPC.toLocal8Bit().data());    //Hello,欢迎来到 UPC
    qDebug(str8.toLocal8Bit().data());
    ui->lineEditOut->setText(str8);

    double pi_num1 = M_PI;     //圆周率常数pi，在<QtMath>中定义
    str8 = QString::asprintf("Pi = %.10f", pi_num1);
    qDebug(str8.toLocal8Bit().data());

    //====测试函数 arg()
    int Y = 2021, M = 8, D = 3;
    int base = 10;
    QChar ch('0');

    QString str9 = QString("%1年度: %3/%2/%1").arg(Y).arg(M, 2, base, ch).arg(D, 2, base, ch);
    //QString str9 = QString("%1年%2月%3日").arg(Y).arg(M, 2, base, ch).arg(D, 2, base, ch);
    qDebug(str9.toLocal8Bit().data());   //有汉字，会乱码
    ui->lineEditOut->setText(str9);

    QString name = "张三";
    int age = 25;
    QString str10 = QString("他名叫%1, 今年%2岁").arg(name).arg(age);
    qDebug(str10.toLocal8Bit().data());   //有汉字，会乱码
    ui->lineEditOut->setText(str10);

    double pi_num2 = M_PI;     //圆周率常数pi，在<QtMath>中定义
    int precision = 8;
    QString str11 = QString("pi = %1").arg(pi_num2, 0, 'f', precision);
    qDebug(str11.toLocal8Bit().data());   //有汉字，会乱码
    ui->lineEditOut->setText(str11);
}

void Widget::on_pushButtonDec_clicked()
{
    int val = ui->lineEditDec->text().toInt(); //读取十进制数
    QString str = QString::number(val, 16); //显示为十六进制字符串
    str = str.toUpper(); //转换为大写字母
    ui->lineEditHex->setText(str);

    str = QString::number(val, 2); //显示为二进制字符串
    ui->lineEditBin->setText(str);
}

void Widget::on_pushButtonBin_clicked()
{
    bool ok;
    int val = ui->lineEditBin->text().toInt(&ok, 2);

    QString str = QString::number(val, 10);
    ui->lineEditDec->setText(str);

    str = QString::number(val, 16);
    str = str.toUpper();
    ui->lineEditHex->setText(str);
}

void Widget::on_pushButtonHex_clicked()
{
    bool ok;
    int val = ui->lineEditHex->text().toInt(&ok, 16);

    QString str = QString::number(val, 10);
    ui->lineEditDec->setText(str);

    str = QString::number(val, 2);
    ui->lineEditBin->setText(str);
}

